﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using K2.Sync.Entity;

namespace K2.Sync.Service.Services
{
    public class K2DataService
    {
        /// <summary>
        /// 获取worklist数据
        /// </summary>
        /// <param name="procinstId">流程实例id</param>
        /// <returns></returns>
        public WorklistDataEntity GetWorklistData(int procinstId)
        {
            using (var db = new K2DBContext())
            {
                var sql = @"SELECT     TOP (200) ServerLog.Worklist.ProcInstID, ServerLog.Worklist.ActInstDestID, ServerLog.Worklist.Destination, ServerLog.Worklist.DestType, 
                      ServerLog.Worklist.StartDate, ServerLog.Worklist.EndDate, ServerLog.Worklist.Status, ServerLog.ProcInst.Folio, ServerLog.ProcInst.Originator, 
                      Server.WorklistHeader.Data
FROM         ServerLog.Worklist INNER JOIN
                      ServerLog.ProcInst ON ServerLog.Worklist.ProcInstID = ServerLog.ProcInst.ID INNER JOIN
                      Server.WorklistHeader ON ServerLog.Worklist.ProcInstID = Server.WorklistHeader.ProcInstID
WHERE     (ServerLog.ProcInst.Status = 2) AND (ServerLog.Worklist.ProcInstID = {0})";
                sql = String.Format(sql, procinstId);
                //Console.WriteLine(sql);
                var data = db.Database.SqlQuery<WorklistDataEntity>(sql).FirstOrDefault();
                //Console.WriteLine("查询完成");
                return data;
            }
        }

        /// <summary>
        /// 获取DataField的值
        /// </summary>
        /// <param name="procinstId">流程实例id</param>
        /// <returns></returns>
        public object GetDataFieldData(int procinstId)
        {
            using (var db = new K2DBContext())
            {
                var parameters = new SqlParameter("ProcInstID", procinstId);
                var data = db.Database.SqlQuery<ProcInstDataEntity>("SELECT ProcInstID, Name, Value FROM [ServerLog].[ProcInstData] WHERE ProcInstID=@ProcInstID", parameters).ToList();
                //Console.WriteLine("查询完成");
                return data;
            }
        }
    }
}
